package batch.home;

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.Date;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.*;

import java.util.Vector;
import java.util.Properties;
import java.io.*;
import java.net.*;

import batch.common.report.*;

public class makeHomeForUan
{
    private final int    CNT_RINGTONE  = 5;
    private final int    CNT_IMAGE     = 2;
    private final int    CNT_GAME  = 2;

    private final String TODAY         = DateFormatUtils.format(new Date(), "yyyyMMdd");  //Get Current Date
    private final String IMAGE_URL     = "/images/main/";
    private final String FILE_PATH     = "/home01/sfc/batch/home/data/uan_home/" + TODAY;
    private final String MAILSUBJECT   = "UAN HOME Batch Error(" + TODAY + ")";

    private final String GO_RINGTONES  = "javascript:goMenu('funclub','musictones');";
    private final String RINGTONE_PATH = "link/goPremium.jsp?mcate_cd=ring&content_id=";

    private final String GO_IMAGES     = "javascript:goMenu('funclub','graphics');";
    private final String IMAGE_PATH    = "/link/goPremium.jsp?mcate_cd=graphic&content_id=";

    private final String GO_JAVAGAMES  = "javascript:goMenu('funclub','games');";
    private final String JAVAGAME_PATH = "/link/goPremium.jsp?mcate_cd=game&content_id=";

    private StringBuffer sHtml = new StringBuffer();

    public static void main( String[] args ){
        Logger.getLogger("home.uan_home").info("( * ) Start.");

        makeHomeForUan obj = new makeHomeForUan();

        obj.getContents("A1");
        obj.getContents("08");
        obj.getContents("04");
        Logger.getLogger("home.uan_home").info("( * ) End.");
    }

    public void getContents(String mall_id ){
        BufferedReader bfrString = null;
        ByteArrayInputStream is = null;
        StringBuffer sbfXml = new StringBuffer();

        try{
//          HTTP Connection
            URLConnection urlCon = null ;
            if(mall_id.equals("A1")){
                urlCon = new URL("http://funclub.samsung.ua/xml/GetContentList.php?NUM="+CNT_RINGTONE+"&MALL_ID=A1").openConnection();
            }else if(mall_id.equals("08")){
                urlCon = new URL("http://funclub.samsung.ua/xml/GetContentList.php?NUM="+CNT_IMAGE+"&MALL_ID=08").openConnection();
            }else if(mall_id.equals("04")){
                urlCon = new URL("http://funclub.samsung.ua/xml/GetContentList.php?NUM="+CNT_GAME+"&MALL_ID=04").openConnection();
            }
            bfrString = new BufferedReader(new InputStreamReader(urlCon.getInputStream(), "UTF-8"));

            String line = "";
            Vector vContent   = new Vector();

            while ((line = bfrString.readLine()) != null)
            {
                sbfXml.append(line);
            }

            byte bxml[] = sbfXml.toString().getBytes("UTF-8");
            is = new ByteArrayInputStream(bxml);

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new InputSource(is));

            Element elem = doc.getDocumentElement();
            String root_node = elem.getNodeName();
            NodeList nlTemp = elem.getElementsByTagName("CONTENT");
            int leng = nlTemp.getLength();

            for (int i = 0; i < leng; i++) {
                Properties pptNode = new Properties();
                nlTemp = elem.getElementsByTagName("CONTENT_ID");
                pptNode.setProperty("CONTENT_ID", nlTemp.item(i).getFirstChild().getNodeValue());
                nlTemp = elem.getElementsByTagName("CONTENT_NAME");
                pptNode.setProperty("CONTENT_NAME", nlTemp.item(i).getFirstChild().getNodeValue());
                nlTemp = elem.getElementsByTagName("PREVIEW_URL");
                pptNode.setProperty("PREVIEW_URL", nlTemp.item(i).getFirstChild().getNodeValue());
                nlTemp = elem.getElementsByTagName("ORDER_URL");
                pptNode.setProperty("ORDER_URL", nlTemp.item(i).getFirstChild().getNodeValue());

                vContent.addElement(pptNode);
            }

            File fl = new File(FILE_PATH);

            if(!fl.isDirectory()){
                fl.mkdirs();
            }

            if(vContent.size() > 0){
                if(mall_id.equals("A1")){
                    writeRingtones(vContent);
                }else if(mall_id.equals("08")){
                    writeImages(vContent);
                }else if(mall_id.equals("04")){
                    writeJavaGames(vContent);
                }

            }
        }
        catch(Exception ex){
            ex.printStackTrace();
            Logger.getLogger("home.uan_home").info(ex.toString());
            ReportError.byEmail(MAILSUBJECT, ex.toString());
        }
        finally {
            if (is != null) try { is.close(); } catch (Exception ex) {}
            if (bfrString != null) try { bfrString.close(); } catch (Exception ex) {}
        }
    }

    //Make Ringtone File
    public void writeRingtones(Vector pVector) throws Exception {
        OutputStreamWriter fw = null;

        String sContentNM = "";
        String sContentID = "";
        String sUrl = "";

        sHtml.setLength(0);
        try{
            sHtml.append("<table width=191 cellpadding=0 cells pacing=0>                                                                                     \n")
                 .append("    <tr>                                                                                                                          \n")
                 .append("        <td height=29 style='background-image : url(" + IMAGE_URL + "bg_box02_01.gif)' align=center>                              \n")
                 .append("            <table width=170 cellpadding=0 cellspacing=0>                                                                         \n")
                 .append("                <tr>                                                                                                              \n")
                 .append("                    <td><a href=" + GO_RINGTONES + "><img src='" + IMAGE_URL + "title_ringtones.gif' align=absmiddle></a></td>    \n")
                 .append("                    <td width=36><a href=" + GO_RINGTONES + "><img src='" + IMAGE_URL + "bn_more01.gif' align=absmiddle></a></td> \n")
                 .append("                </tr>                                                                                                             \n")
                 .append("            </table>                                                                                                              \n")
                 .append("        </td>                                                                                                                     \n")
                 .append("    </tr>                                                                                                                         \n")
                 .append("    <tr>                                                                                                                          \n")
                 .append("        <td height=114 style='background-image : url(" + IMAGE_URL + "bg_box02_02.gif);padding:5 0 0 0' align=center valign=top>  \n")
                 .append("            <table width=183 cellpadding=0 cellspacing=0>                                                                         \n")
                 .append("                <col width=8>                                                                                                     \n")
                 .append("                <col width=160>                                                                                                   \n")
                 .append("                <col width=10>                                                                                                    \n")
                 .append("                <col width=5>                                                                                                     \n");

            for(int i=0; i < CNT_RINGTONE; i++) {
                Properties pContents = (Properties) pVector.elementAt(i);
                
                sContentNM = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(StringUtils.defaultString(pContents.getProperty("CONTENT_NAME")));
                sContentNM = StringUtils.abbreviate(sContentNM, 26);

                sContentID = StringUtils.defaultString(pContents.getProperty("CONTENT_ID"));
                sUrl = RINGTONE_PATH + sContentID;
                
                sHtml.append("            <tr bgcolor=#F7FEFF>                                                                                              \n")
                     .append("                <td><img src='" + IMAGE_URL + "bg_box02_03.gif' align=absmiddle></td>                                         \n")
                     .append("                <td><a href='" + sUrl + "' >" + sContentNM + "</a></td>                                              \n")
                     .append("                <td><a href='javascript:playMusic(" + sContentID + ");' ><img src='" + IMAGE_URL + "bn_sound.gif' align=absmiddle></a></td>       \n")
                     .append("                <td align=right><img src='" + IMAGE_URL + "bg_box02_04.gif' align=absmiddle></td>                             \n")
                     .append("            </tr>                                                                                                             \n");


                if ( i < (CNT_RINGTONE - 1) ) {
                    sHtml.append("        <tr><td colspan=4 height=1></td></tr>                                                                             \n");
                }

            }

            sHtml.append("            </table>                                                                                                              \n")
                 .append("        </td>                                                                                                                     \n")
                 .append("    </tr>                                                                                                                         \n")
                 .append("</table>                                                                                                                          \n");

            fw = new OutputStreamWriter(new FileOutputStream(FILE_PATH + "/ringtone_include.html", false), "UTF-8");
            fw.write(sHtml.toString());

            fw.close();
            fw = null;
        }
        finally{
            if (fw != null) try { fw.close(); } catch (Exception e) {}
        }
    }

    //Make Images File
    public void writeImages(Vector pVector) throws Exception {
        OutputStreamWriter fw = null;

        String[][] asContents = new String[CNT_IMAGE][3];

        sHtml.setLength(0);

        try{
            for(int i=0; i<asContents.length; i++) {
                Properties pContents = (Properties) pVector.elementAt(i);

                asContents[i][0] = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(StringUtils.defaultString(pContents.getProperty("CONTENT_NAME")));
                asContents[i][0] = StringUtils.abbreviate(asContents[i][0], 15);

                asContents[i][1] = pContents.getProperty("PREVIEW_URL");
                asContents[i][2] = IMAGE_PATH + pContents.getProperty("CONTENT_ID");
            }

            sHtml.append("<table width=191 cellpadding=0 cellspacing=0>                                                                                                                                               \n")
                 .append("    <tr height=29>                                                                                                                                                                          \n")
                 .append("        <td style='background-image : url(" + IMAGE_URL + "bg_title_02.gif)' align=center >                                                                                                 \n")
                 .append("            <table width=170 cellpadding=0 cellspacing=0>                                                                                                                                   \n")
                 .append("                <tr>                                                                                                                                                                        \n")
                 .append("                    <td><a href=" + GO_IMAGES + "><img src='" + IMAGE_URL + "title_screensavers.gif' align=absmiddle></a></td>                                                              \n")
                 .append("                    <td width=36><a href=" + GO_IMAGES + "><img src='" + IMAGE_URL + "bn_more03.gif' align=absmiddle></a></td>                                                              \n")
                 .append("                </tr>                                                                                                                                                                       \n")
                 .append("            </table>                                                                                                                                                                        \n")
                 .append("        </td>                                                                                                                                                                               \n")
                 .append("    </tr>                                                                                                                                                                                   \n")
                 .append("    <tr><td height=2></td></tr>                                                                                                                                                             \n")
                 .append("    <tr>                                                                                                                                                                                    \n")
                 .append("        <td height=112 style='background-image : url(" + IMAGE_URL + "bg_box03.gif);padding:13 0 0 0' align=center valign=top>                                                              \n")
                 .append("            <table width=166 cellpadding=0 cellspacing=0>                                                                                                                                   \n")
                 .append("                <tr>                                                                                                                                                                        \n")
                 .append("                    <td>                                                                                                                                                                    \n")
                 .append("                        <table width=79 height=79 cellpadding=1 cellspacing=0 bgcolor=#9AA9B4>                                                                                              \n")
                 .append("                            <tr>                                                                                                                                                            \n")
                 .append("                                <td><a href=" + asContents[0][2] + "><img src='" + asContents[0][1] + "' width=77 height=77 align=absmiddle></a></td>                                       \n")
                 .append("                            </tr>                                                                                                                                                           \n")
                 .append("                        </table>                                                                                                                                                            \n")
                 .append("                    </td>                                                                                                                                                                   \n")
                 .append("                    <td width=8></td>                                                                                                                                                       \n")
                 .append("                    <td>                                                                                                                                                                    \n")
                 .append("                        <table width=79 height=79 cellpadding=1 cellspacing=0 bgcolor=#9AA9B4>                                                                                              \n")
                 .append("                            <tr>                                                                                                                                                            \n")
                 .append("                                <td><a href=" + asContents[1][2] + "><img src='" + asContents[1][1] + "' width=77 height=77 align=absmiddle></a></td>                                       \n")
                 .append("                            </tr>                                                                                                                                                           \n")
                 .append("                        </table>                                                                                                                                                            \n")
                 .append("                    </td>                                                                                                                                                                   \n")
                 .append("                </tr>                                                                                                                                                                       \n")
                 .append("                <tr>                                                                                                                                                                        \n")
                 .append("                    <td class=f_09 style='padding:3 0 0 0'><img src='" + IMAGE_URL + "icon_02.gif' align=absmiddle>&nbsp;<a href=" + asContents[0][2] + ">" + asContents[0][0] + "</a></td> \n")
                 .append("                    <td></td>                                                                                                                                                               \n")
                 .append("                    <td class=f_09 style='padding:3 0 0 0'><img src='" + IMAGE_URL + "icon_02.gif' align=absmiddle>&nbsp;<a href=" + asContents[1][2] + ">" + asContents[1][0] + "</a></td> \n")
                 .append("                </tr>                                                                                                                                                                       \n")
                 .append("            </table>                                                                                                                                                                        \n")
                 .append("        </td>                                                                                                                                                                               \n")
                 .append("    </tr>                                                                                                                                                                                   \n")
                 .append("</table>                                                                                                                                                                                    \n");

            fw = new OutputStreamWriter(new FileOutputStream(FILE_PATH + "/image_include.html", false), "UTF-8");
            fw.write(sHtml.toString());
            
            fw.close();
            fw = null;
        }
        finally{
            if (fw != null) try { fw.close(); } catch (Exception e) {}
        }
    }


    //Make JavaGames File
    public void writeJavaGames(Vector pVector) throws Exception {
        OutputStreamWriter fw = null;

        String[][] asContents = new String[CNT_GAME][3];

        sHtml.setLength(0);

        try{
            for(int i=0; i<asContents.length; i++) {
                Properties pContents = (Properties) pVector.elementAt(i);

                asContents[i][0] = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(StringUtils.defaultString(pContents.getProperty("CONTENT_NAME")));
                asContents[i][0] = StringUtils.abbreviate(asContents[i][0], 15);

                asContents[i][1] = pContents.getProperty("PREVIEW_URL");
                asContents[i][2] = JAVAGAME_PATH + pContents.getProperty("CONTENT_ID");
            }

            sHtml.append("<table width=191 cellpadding=0 cellspacing=0>                                                                                                                                               \n")
                 .append("    <tr height=29>                                                                                                                                                                          \n")
                 .append("        <td style='background-image : url(" + IMAGE_URL + "bg_title_01.gif)' align=center >                                                                                                 \n")
                 .append("            <table width=170 cellpadding=0 cellspacing=0>                                                                                                                                   \n")
                 .append("                <tr>                                                                                                                                                                        \n")
                 .append("                    <td><a href=" + GO_JAVAGAMES + "><img src='" + IMAGE_URL + "title_mobile_games.gif' align=absmiddle></a></td>                                                           \n")
                 .append("                    <td width=36><a href=" + GO_JAVAGAMES + "><img src='" + IMAGE_URL + "bn_more02.gif' align=absmiddle></a></td>                                                           \n")
                 .append("                </tr>                                                                                                                                                                       \n")
                 .append("            </table>                                                                                                                                                                        \n")
                 .append("        </td>                                                                                                                                                                               \n")
                 .append("    </tr>                                                                                                                                                                                   \n")
                 .append("    <tr><td height=2></td></tr>                                                                                                                                                             \n")
                 .append("    <tr>                                                                                                                                                                                    \n")
                 .append("        <td height=112 style='background-image : url(" + IMAGE_URL + "bg_box03.gif);padding:13 0 0 0' align=center valign=top>                                                              \n")
                 .append("            <table width=166 cellpadding=0 cellspacing=0>                                                                                                                                   \n")
                 .append("                <tr>                                                                                                                                                                        \n")
                 .append("                    <td>                                                                                                                                                                    \n")
                 .append("                        <table width=79 height=79 cellpadding=1 cellspacing=0 bgcolor=#9AA9B4>                                                                                              \n")
                 .append("                            <tr>                                                                                                                                                            \n")
                 .append("                                <td><a href=" + asContents[0][2] + "><img src='" + asContents[0][1] + "' width=77 height=77 align=absmiddle></a></td>                                       \n")
                 .append("                            </tr>                                                                                                                                                           \n")
                 .append("                        </table>                                                                                                                                                            \n")
                 .append("                    </td>                                                                                                                                                                   \n")
                 .append("                    <td width=8></td>                                                                                                                                                       \n")
                 .append("                    <td>                                                                                                                                                                    \n")
                 .append("                        <table width=79 height=79 cellpadding=1 cellspacing=0 bgcolor=#9AA9B4>                                                                                              \n")
                 .append("                            <tr>                                                                                                                                                            \n")
                 .append("                                <td><a href=" + asContents[1][2] + "><img src='" + asContents[1][1] + "' width=77 height=77 align=absmiddle></a></td>                                       \n")
                 .append("                            </tr>                                                                                                                                                           \n")
                 .append("                        </table>                                                                                                                                                            \n")
                 .append("                    </td>                                                                                                                                                                   \n")
                 .append("                </tr>                                                                                                                                                                       \n")
                 .append("                <tr>                                                                                                                                                                        \n")
                 .append("                    <td class=f_09 style='padding:3 0 0 0'><img src='" + IMAGE_URL + "icon_02.gif' align=absmiddle>&nbsp;<a href=" + asContents[0][2] + ">" + asContents[0][0] + "</a></td> \n")
                 .append("                    <td></td>                                                                                                                                                               \n")
                 .append("                    <td class=f_09 style='padding:3 0 0 0'><img src='" + IMAGE_URL + "icon_02.gif' align=absmiddle>&nbsp;<a href=" + asContents[1][2] + ">" + asContents[1][0] + "</a></td> \n")
                 .append("                </tr>                                                                                                                                                                       \n")
                 .append("            </table>                                                                                                                                                                        \n")
                 .append("        </td>                                                                                                                                                                               \n")
                 .append("    </tr>                                                                                                                                                                                   \n")
                 .append("</table>                                                                                                                                                                                    \n");

            fw = new OutputStreamWriter(new FileOutputStream(FILE_PATH + "/game_include.html", false), "UTF-8");
            fw.write(sHtml.toString());
            
            fw.close();
            fw = null;
        }
        finally{
            if (fw != null) try { fw.close(); } catch (Exception e) {}
        }
    }
}
